﻿//-----------------------------------------------------------------------
// <copyright file="GoatGenTask.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace GoatGenBuildTask
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using GoatGen.Generator;
    using GoatGen.Object;
    using GoatGen.Token;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// Code generation task used to integrate with the MSBuild system.
    /// </summary>
    public class GoatGenTask : Task
    {
        #region Properties - Public

        /// <summary>
        /// Gets or sets the string which contains the files we will generate from.
        /// Files are separated by a ;.
        /// </summary>
        [Required]
        public string Files
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the string which contains our output files. Output files are assumed
        /// to be in the same order as the input files.
        /// 
        /// Files are separated by a ;.
        /// </summary>
        [Required]
        public string Outputs
        {
            get;
            set;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Execute the task.
        /// </summary>
        /// <returns>Returns true if the task was successful.</returns>
        public override bool Execute()
        {
            if (string.IsNullOrEmpty(this.Files))
            {
                throw new ArgumentNullException("Input files string must not be null or empty.");
            }

            if (string.IsNullOrEmpty(this.Outputs))
            {
                throw new ArgumentNullException("Output files string must not be null or empty.");
            }

            string[] processFiles = this.Files.Split(';');
            string[] outputFiles = this.Outputs.Split(';');

            if (processFiles.Length != outputFiles.Length)
            {
                throw new ArgumentException("Input file length is not the same as the output file length.");
            }

            for (int i = 0; i < processFiles.Length; ++i)
            {
                this.ProcessFile(processFiles[i], outputFiles[i]);
            }

            return true;
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Process the passed input file and store the results in the provided output file.
        /// </summary>
        /// <param name="inputFile">Input file.</param>
        /// <param name="outputFile">Output file.</param>
        private void ProcessFile(string inputFile, string outputFile)
        {
            GoatTokenizer tokenizer = new GoatTokenizer(inputFile);

            IList<GoatToken> tokens = tokenizer.Tokenize();

            GoatObjectBuilder builder = new GoatObjectBuilder(tokens);
            GoatObject obj = builder.Build();
            GoatCodeGenerator generator = new GoatCodeGenerator(obj);

            using (StreamWriter writer = new StreamWriter(outputFile))
            {
                generator.Generate(writer);
            }

            this.Log.LogMessage(MessageImportance.High, "GoatGen: " + inputFile + " to " + outputFile);
        }

        #endregion
    }
}